/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgep.actions;

import sgep.params.Params;
import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 *
 * @author mg
 */
public class Init extends ActionSupport {

    private String type;
    private String message;
    private ResourceBundleMessageSource messageSource;

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @Override
    public String execute() throws Exception {
        if (type == null || type.equals("")) {
            message = messageSource.getMessage("message.nologin", null, Locale.getDefault());
            return SUCCESS;
        }
        String s;

        s = messageSource.getMessage("params.app_home", null, Locale.getDefault());
        if (s != null) {
            Params.APP_HOME_DIR = s;
        }
        s = messageSource.getMessage("params.sgp_root", null, Locale.getDefault());
        if (s != null) {
            Params.SGP_ROOT = s;
            Params.SGE_EXPORT[4] = "SGP_ROOT=" + s;
        }
        s = messageSource.getMessage("params.sge_root", null, Locale.getDefault());
        if (s != null) {
            Params.SGE_ROOT = s;
            Params.SGE_EXPORT[0] = "SGE_ROOT=" + s;
        }
        s = messageSource.getMessage("params.sge_cell", null, Locale.getDefault());
        if (s != null) {
            Params.SGE_CELL = s;
            Params.SGE_EXPORT[1] = "SGE_CELL=" + s;
        }
        s = messageSource.getMessage("params.commd_port", null, Locale.getDefault());
        if (s != null) {
            Params.COMMD_PORT = s;
            Params.SGE_EXPORT[2] = "COMMD_PORT=" + s;
        }
        //Determine SGE architecture
        try {
            Process sgeArch =
                    Runtime.getRuntime().exec(Params.SGE_ROOT + "util/arch", Params.SGE_EXPORT);
            BufferedReader archStdout =
                    new BufferedReader(new InputStreamReader(sgeArch.getInputStream()));

            Params.SGE_ARCH = archStdout.readLine();
            archStdout.close();
        } catch (IOException ioe) {
            Params.SGE_ARCH = "solaris";
        }

        Params.SGE_EXPORT[3] = "LD_LIBRARY_PATH=" + Params.SGE_ROOT + "lib/" + Params.SGE_ARCH;

        if (type.equals("user")) {
            // Parameters for X tunneling and vncserver script
            s = messageSource.getMessage("params.x_netlet", null, Locale.getDefault());
            if (s != null) {
                Params.X_NETLET = s;
            }
            s = messageSource.getMessage("params.x_server", null, Locale.getDefault());
            if (s != null) {
                Params.X_SERVER = s;
            } else {
                try {
                    Params.X_SERVER = InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException uhe) {
                    Params.X_SERVER = "localhost";
                }
            }
            s = messageSource.getMessage("params.vncroot", null, Locale.getDefault());
            if (s != null) {
                Params.VNCSERVER = s + "/vncserver";
            }



            Params.GETWORKSPACE = Params.SGP_ROOT + "bin/gethomedir " + Params.SGP_ROOT + " ";
            Params.ADMINRUN = Params.SGE_ROOT + "utilbin/" + Params.SGE_ARCH + "/adminrun ";
            return "user";
        }else{
            return "admin";
        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
